package com.legal.controller.practice;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.legal.domain.po.practice_view.QuestionsByCategoryNoView;
import com.legal.domain.result.Result;
import com.legal.service.practice.impl.view.IQuestionsByCategoryNoViewService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 刷题功能模块 - 问题管理 控制器
 * </p>
 *
 * @author xiaopeng
 * @since 2025-06-27
 */
@RestController
@RequestMapping("/question")
@RequiredArgsConstructor
@Tag(name = "问题管理")  // OpenAPI 3.0 注解
public class QuestionController {

    private final IQuestionsByCategoryNoViewService questionsByCategoryNoViewService;

    /**
     * 获取指定分类下的所有问题列表
     *
     * @param categoryNo
     * @return
     */
    @Operation(summary = "获取指定分类下的所有问题列表")
    @GetMapping("/list")
    public Result<List<QuestionsByCategoryNoView>> list(@RequestParam("categoryNo") String categoryNo) {
        return Result.ok(questionsByCategoryNoViewService.getBaseMapper()
                .selectList(new QueryWrapper<QuestionsByCategoryNoView>().eq("category_no", categoryNo)));
    }


    /**
     * 根据指定的数量，随机获取指定分类下的指定数量的问题列表
     *
     * @param categoryNo
     * @return
     */
    @Operation(summary = "随机获取指定分类下的指定数量的问题列表")
    @GetMapping("/limitList")
    public Result<List<QuestionsByCategoryNoView>> list(@RequestParam("categoryNo") String categoryNo,
                                                        @RequestParam("limit") Integer limit) {
        return Result.ok(questionsByCategoryNoViewService.getBaseMapper()
                .selectList(new QueryWrapper<QuestionsByCategoryNoView>().eq("category_no", categoryNo)
                        .last("ORDER BY RAND() LIMIT " + limit))); // 随机排序后 取指定数量
    }


}
